﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Arroguella.Tiles;
using Microsoft.Xna.Framework;

namespace Arroguella.Entities
{
    /// <summary>
    /// A CreatureEntity is an Entity that is some form of NPC, animal, monster, or living thing.
    /// It can (typically) be defeated for XP.
    /// </summary>
    public class CreatureEntity : Entity
    {
        #region Constructors

        public CreatureEntity(string name, char symbol, Color color, int level)
            : base(name, symbol, color)
        {
            int cr = level + 1; // "Challenge Rating"
            Level = cr;
            Strength = cr * random.Next(1, 2 * level + 1);
            Agility = cr * random.Next(1, 2 * level + 1);
            Intelligence = cr * random.Next(1, 2 * level + 1);
            MaxHealth = cr * random.Next(1, 2 * level + 1);
            CurrentHealth = MaxHealth;
            MaxMana = cr * random.Next(1, 2 * level + 1);
            CurrentMana = MaxMana;
            Xp = cr * 15 + Strength + Agility + Intelligence + MaxHealth + MaxMana;

            IsVisible = true;
        }

        #endregion

        #region Events
        #endregion

        #region Methods

        public virtual void Update(Tile tile)
        {
            // Try to see if we have an applicable AI component
            if (Ai == null)
            {
                foreach (EntityComponent component in Components.Where(component => component is AiComponent))
                {
                    Ai = component as AiComponent;
                    break;
                }

                // Safety check
                if (Ai == null)
                {
                    return;
                }
            }

            Ai.Update(tile);
        }

        /// <summary>
        /// What to do when the creature entity dies
        /// </summary>
        public virtual void Die()
        {
            Symbol = '%';
            Name += " remains";
            Color = Color.DarkGreen;
            CurrentHealth = 0;
            RemoveAiComponents();
        }

        private void RemoveAiComponents()
        {
            Components.RemoveAll(component => component is AiComponent);
            Ai = null;
        }

        #endregion

        #region Properties

        /// <summary>
        /// The creature level.
        /// </summary>
        public int Level { get; set; }

        /// <summary>
        /// The amount of XP awarded when the creature is defeated
        /// </summary>
        public int Xp { get; set; }

        public AiComponent Ai { get; set; }

        #region Attributes

        /// <summary>
        /// creature strength, affects damage, health and strength-based skills.
        /// </summary>
        public int Strength { get; set; }

        /// <summary>
        /// creature agility, affects defense, accuracy and agility-based skills.
        /// </summary>
        public int Agility { get; set; }

        /// <summary>
        /// creature intelligence, affects mana and intelligence/knowledge-based skills.
        /// </summary>
        public int Intelligence { get; set; }

        /// <summary>
        /// The total amount of hit points the creature can have.
        /// </summary>
        public int MaxHealth { get; set; }

        /// <summary>
        /// The current amount of hit points the creature has.
        /// </summary>
        public int CurrentHealth { get; set; }

        /// <summary>
        /// The total amount of mana the creature can have.
        /// </summary>
        public int MaxMana { get; set; }

        /// <summary>
        /// The current amount of mana the creature has.
        /// </summary>
        public int CurrentMana { get; set; }

        public float Defense { get { return Agility + 10; } }

        #endregion

        private Random random = new Random();

        #endregion

    }
}
